package cn.wang.user.service;

import cn.wang.common.Exceptions.impl.LyException;
import cn.wang.common.enums.impl.ExceptionEnum;
import cn.wang.common.utils.NumberUtils;
import cn.wang.user.mapper.UserMapper;
import cn.wang.user.utils.CodecUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import user.pojo.User;

import java.util.Date;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private StringRedisTemplate redisTemplate;
    private final String key_prefix = "user:verify:phone:";

    /**
     * 校验用 户名/手机号 是否唯一
     * @param data
     * @param type
     * @return
     */
    public boolean checkUnique(String data, Integer type) {
        User user = new User();
        switch (type) {
            case 1:
                user.setUsername(data);
                break;
            case 2:
                user.setPhone(data);
                break;
            default:
                throw new LyException(ExceptionEnum.INVALID_USER_PARAM);
        }
        int count = userMapper.selectCount(user);
        return count==0;
    }

    /**
     * 生产验证码
     * @param phone
     */
    public void createCode(String phone) {
        HashMap<String, String> map = new HashMap<>();
        String code = NumberUtils.generateCode(6);
        map.put("phoneNumber",phone);
        map.put("code",code);
        rabbitTemplate.convertAndSend("ly.sms.exchange","sms.register",map);
        // 在redis中保存数据
        String key = key_prefix + phone;
        redisTemplate.opsForValue().set(key,code,5, TimeUnit.MINUTES);
    }

    public void register(User user, String code) {
        // 0.获取验证码
        String key = key_prefix + user.getPhone();
        String redisCode = redisTemplate.opsForValue().get(key);
        // 1.校验验证码
        if (!StringUtils.equals(code,redisCode)){
            return;
        }
        // 2.生成随机盐
        String salt = CodecUtils.generateSalt();
        user.setSalt(salt);
        // 3.加密密码
        user.setPassword(CodecUtils.md5Hex(user.getPassword(),salt));
        // 4.存入数据库
        user.setId(null);
        user.setCreated(new Date());
        userMapper.insert(user);
        // 5.删除验证码
        redisTemplate.delete(key);
    }

    public User queryUser(String username, String password) {
        if (StringUtils.isBlank(username) || StringUtils.isBlank(password)) {
            throw new LyException(ExceptionEnum.USERNAME_OR_PASSWORD_IS_NULL);
        }
        // 1. 通过用户名查询用户
        User record = new User();
        record.setUsername(username);
        User user = userMapper.selectOne(record);

        if (user == null) {
            throw new LyException(ExceptionEnum.USERNAME_ERROR);
        }

        // 2. 加密用户输入的密码
        password = CodecUtils.md5Hex(password, user.getSalt());
        // 3. 比较密码密码
        if (!StringUtils.equals(password,user.getPassword())){
            throw new LyException(ExceptionEnum.PASSWORD_ERROR);
        }
        return user;
    }
}
